package com.example.projectdemo.p1;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.projectdemo.R;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/***
 * 创建时间：2020/2/24 17:41
 * 创建人：10850
 * 功能描述：Android知乎选项卡动态隐藏
 * https://www.jianshu.com/p/b25c9604fbe0
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    //数据源
    private List<String> mList;
    private Context mContext;

    private boolean isUp;

    private AtomicBoolean doAnimation = new AtomicBoolean(false);

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    //构造方法
    public MyAdapter(Context context, List<String> list) {
        this.mList = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.citem, viewGroup, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
        viewHolder.mTextView.setText(mList.get(i));

//        ViewGroup.LayoutParams layoutParams = viewHolder.mTextView.getLayoutParams();
//        layoutParams.height = new Random().nextInt(200) + 200;
//        viewHolder.mTextView.setLayoutParams(layoutParams);
        //点击事件
        if (mOnItemClickListener != null) {
            viewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(viewHolder.mTextView, pos);
                }
            });

            viewHolder.mTextView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = viewHolder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(viewHolder.mTextView, pos);
                    return false;
                }
            });
        }


        if (isUp) { //上拉
            Animator[] animators = getAnimators(viewHolder.itemView);
            if (animators.length > 1) {
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(animators);
                animatorSet.start();
            } else {
                for (Animator annimator : animators) {
                    annimator.start();
                }
            }
            Log.i("zxd", "onBindViewHolder: shang");
        } else {//下拉
            Animator[] animatorsDown = getAnimatorsDown(viewHolder.itemView);
            if (animatorsDown.length > 1) {
                if (doAnimation.get()) return;//如果正在 动画进行中 ,不能开始动画
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(animatorsDown);
                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        doAnimation.set(true);
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        doAnimation.set(false);
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        doAnimation.set(false);
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                animatorSet.start();
            } else {
                if (doAnimation.get()) return;//如果正在 动画进行中 ,不能开始动画
                for (Animator annimator : animatorsDown) {
                    annimator.addListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            doAnimation.set(true);
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            doAnimation.set(false);
                        }

                        @Override
                        public void onAnimationCancel(Animator animation) {
                            doAnimation.set(false);
                        }

                        @Override
                        public void onAnimationRepeat(Animator animation) {

                        }
                    });
                    annimator.start();
                }
            }
            Log.i("zxd", "onBindViewHolder: xia");
        }


    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mTextView;
        FrameLayout mFrameLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.text_view);
            mFrameLayout = itemView.findViewById(R.id.fl_view);
        }
    }

    //添加item
    public void addData(int position) {
        mList.add(position, "Insert One");
        notifyItemInserted(position);
    }

    //删除item
    public void removeData(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }


    //给rv添加上滑和下滑的动画
    protected Animator[] getAnimators(View view) {  //上滑动画
        return new Animator[]{
                ObjectAnimator.ofFloat(view, View.ROTATION, 120, 0).setDuration(400)
        };
    }

    protected Animator[] getAnimatorsDown(View view) {  //下拉动画
        return new Animator[]{
                //ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, -100, 0).setDuration(400),
                ObjectAnimator.ofFloat(view, View.SCALE_X, 0.5f, 1f).setDuration(400)
        };
    }

    public void setStatus(boolean status) {
        isUp = status;
    }
}